package cn.bdqn.controller.emp;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FilenameUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import cn.bdqn.pojo.Depatement;
import cn.bdqn.pojo.Emp;
import cn.bdqn.service.emp.EmpService;

@Controller
public class EmpController {
	private Logger logger=Logger.getLogger(EmpController.class);
	@Resource
	private EmpService empservice;
	private Emp emp=null;
	@RequestMapping("/list")
	public String list(Model model, HttpServletRequest rq){
		emp=new Emp();
		String empName=rq.getParameter("empName");
		String depId=rq.getParameter("depId");
		String pageIndex = rq.getParameter("pageIndex");
		if (pageIndex == null) {
			pageIndex = "1";
		}
		if (depId == null) {
			depId = "0";
		}
		int pageCurNO = Integer.parseInt(pageIndex);
		List<Emp> list = empservice.list(empName,Integer.parseInt(depId),(pageCurNO - 1) * 5, 5);
		int getcount = empservice.count(empName,Integer.parseInt(depId));
		int count = getcount % 5 == 0 ? getcount / 5 : getcount / 5 + 1;
		List<Depatement> list1=empservice.getDepList();
		model.addAttribute("listUser", list);
		model.addAttribute("pageIndex", pageIndex);
		model.addAttribute("count", count);
		model.addAttribute("deplist", list1);
		
		return "emplist";
	}
	@RequestMapping("/viewemp")
	public String aa(Model model, HttpServletRequest rq){
		String id=rq.getParameter("id");
		emp=empservice.view(Integer.parseInt(id));
		model.addAttribute("emp", emp);
		return "viewemp";
	}
	@RequestMapping("/addemp")
	public String aaa(Model model, HttpServletRequest rq){
	
		return "empadd";
	}
	@RequestMapping("/empadd")
	public String aaaa(Model model, HttpServletRequest rq,@RequestParam(value="picPath",required=false) MultipartFile attach,HttpServletRequest request){
		String picPath=null;
		String picLocPath=null;
		if(!attach.isEmpty()){
			//定义目标上传路径
			String path=rq.getSession().getServletContext().getRealPath("statics"+File.separator+"uploadfiles");
			String oldFileName=attach.getOriginalFilename();
			String prefix=FilenameUtils.getExtension(oldFileName);
			int filesize=500000;
			if(attach.getSize()>filesize){
				rq.setAttribute("uploadFileError", "*上传大小不得超过500K");
				return "empadd";
			}else if(prefix.equalsIgnoreCase("jpg")
					||prefix.equalsIgnoreCase("jpeg")
					||prefix.equalsIgnoreCase("png")
					||prefix.equalsIgnoreCase("pneg")){
				//当前系统时间+随机数+"_Personal.jpg"
				String fileName=System.currentTimeMillis()
						+"_Personal.jpg";
				File targetFile=new File(path,fileName);
				if(!targetFile.exists()){
					targetFile.mkdirs();
				}
					try {
						attach.transferTo(targetFile);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						rq.setAttribute("uploadFileError", "*上传失败");
						return "addemp";
					}	
					picPath=path+File.separator+fileName;
					picLocPath = request.getContextPath()+"/statics/uploadfiles/"+fileName;
			}else{
				rq.setAttribute("uploadFileError", "*上传图片格式不正确");
				return "empadd";
			}
		}
		String empName=rq.getParameter("a");
		String phone=rq.getParameter("b");
		String idCardNo=rq.getParameter("c");
		String depId=rq.getParameter("d");
		String jobId=rq.getParameter("e");
		String address=rq.getParameter("f");
		String status=rq.getParameter("g");
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date birthdate = new Date();
		try{
			birthdate=df.parse((String)rq.getParameter("h"));
		}
		catch(Exception e){
		} 
		Emp emp1=new Emp();
		emp1.setEmpName(empName);
		emp1.setPhone(phone);
		emp1.setIdCardNo(idCardNo);
		emp1.setDepId(Integer.parseInt(depId));
		emp1.setJobId(Integer.parseInt(jobId));
		emp1.setAddress(address);
		emp1.setStatus(Integer.parseInt(status));
		emp1.setBirthdate(birthdate);
		emp1.setPicPath(picPath);
		emp1.setPicLocPath(picLocPath);
		int aa=empservice.add(emp1);
		if(aa>0){
			return "redirect:/list";
		}
			return "login";
	}
}
